<?php

namespace mpend\modules\team\models;

use Yii;
use common\models\user\User;
use common\models\workbook\WorkbookMember;
use common\models\workbook\WorkbookInvite;
use common\logics\WorkbookLogic;

/**
 * 班组记工本加入模型
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class BookMemberInviteForm extends \common\base\Model {

    /**
     * @var integer 
     */
    public $user_id;

    /**
     * @var integer 班组长/工头姓名
     */
    public $workbook_id;

    /**
     * @var integer 成员ID
     */
    public $member_id;

    /**
     * @var string 记工薄类型
     */
    public $type = WorkbookInvite::TYPE_JOIN;

    /**
     * @var WorkbookLogic 记工薄
     */
    private $_workbook;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['workbook_id', 'member_id', 'user_id'], 'integer'],
            [['user_id', 'type'], 'required'],
            [['type'], 'in', 'range' => WorkbookInvite::getConstants('type'), 'message' => '{attribute}不在允许范围内'],
            [
                'workbook_id', 'required', 'when' => function ($model) {
                    return empty($model->member_id);
                }
            ],
            [
                'member_id', 'required', 'when' => function ($model) {
                    return $model->type == WorkbookInvite::TYPE_BIND;
                }
            ],
            [['workbook_id'], 'validateWorkbook'],
            [['member_id'], 'validateMember'],
            [['user_id'], 'validateManager'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'title' => '记工本名称',
            'headman' => '班组长/工头姓名',
            'teamwork' => '是否允许工人记工',
        ];
    }

    public function validateWorkbook($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $value = $this->$attribute;
        if ($value) {
            $model = WorkbookLogic::findOne($value);
            if (!$model) {
                return $this->addError($attribute, '记工薄信息不存在');
            }
            $this->_workbook = $model;
        }
    }

    public function validateMember($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $value = $this->$attribute;
        if ($value) {
            $model = WorkbookMember::findOne($value);
            if (!$model || !$model->isActived()) {
                return $this->addError($attribute, '成员信息不存在');
            }
            if (!$model->workbook) {
                return $this->addError($attribute, '记工薄信息不存在');
            }
            $this->workbook_id = $model->workbook_id;
            $this->_workbook = $model->workbook;
        }
    }

    public function validateManager($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $book = $this->getWorkbook();
        if (!$book->hasManageControl($this->$attribute)) {
            return $this->addError($attribute, '您没有该记工簿邀请加入的权限');
        }
    }

    /**
     * @return WorkbookLogic
     */
    public function getWorkbook() {
        return $this->_workbook;
    }

    public function getContent() {
        if ($this->type == WorkbookInvite::TYPE_JOIN) {
            return '我创建了一个班组记工本，邀请你加入，实时记工、快捷对账';
        } else if ($this->type == WorkbookInvite::TYPE_MANAGE) {
            return '我创建了一个班组记工本，邀请你共同管理共享记工本，实时记工、快捷对账';
        } else {
            return '我创建了一个班组记工本，邀请绑定你的账号，实时记工、快捷对账';
        }
    }

    public function getShareTitle() {
        if ($this->type == WorkbookInvite::TYPE_JOIN) {
            return '我创建了一个班组记工本，邀请你加入，实时记工、快捷对账';
        } else if ($this->type == WorkbookInvite::TYPE_MANAGE) {
            return '我创建了一个班组记工本，邀请你共同管理共享记工本，实时记工、快捷对账';
        } else {
            return '我创建了一个班组记工本，邀请绑定你的账号，实时记工、快捷对账';
        }
    }

    public function getDefaultShareData() {
        return [
            'title' => Yii::$app->name . '，随手记工 极速对账 方便快捷 最好用的记工神器',
            'imageUrl' => Url::attach('/assets/share/foreman.jpg'),
            'path' => Url::to(['/pages/index/main'])
        ];
    }

    public function getInviteShareData() {
        return [
            'title' => Yii::$app->name . '，随手记工 极速对账 方便快捷 最好用的记工神器',
            'imageUrl' => Url::attach('/assets/share/foreman.jpg'),
            'path' => Url::to(['/pages/index/main'])
        ];
    }

    public function save() {
        if (!$this->validate()) {
            return $this->getDefaultShareData();
        }
        $model = WorkbookInvite::findOrNew([
            'workbook_id' => $this->workbook_id,
            'user_id' => $this->user_id,
            'member_id' => (int) $this->member_id,
            'type' => $this->type,
            'status' => WorkbookInvite::STATUS_ACTIVE,
            'deleted_at' => 0,
        ]);
        if ($model->isNewRecord) {
            $model->description = $this->getContent();
            if (!$model->save()) {
                return $this->getDefaultShareData();
            }
        }
        return $model->shareData;
    }

}
